﻿using SIG.Model;
using SIG.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SIG.Service;

namespace SIG.WebUI.Areas.Admin.Controllers
{
   // [Authorize]    
    public class MenuController : Controller
    {
     

        private readonly IMenuService _menuService;
        public MenuController(IMenuService menuService)
        {
            _menuService = menuService;
        }
        //
        // GET: /Admin/Menu/
        // [Authorize(Roles = "创始人,管理员")]
        public ActionResult Index()
        {
            IEnumerable<Menu> menuList = _menuService.GetAllMenus();
            return View(menuList);
        }


        // [Authorize(Roles = "创始人,管理员")]

        public ActionResult Add()
        {
             Menu vMenu = new Menu {Active = true, Importance = 0};


             GetParentMenu();

             return View("Add", vMenu);
        }

        private void GetParentMenu()
        {
            var parentMenus = _menuService.GetAllMenus();
            var lMenus = parentMenus.Select(r => new SelectListItem()
            {
                Value = r.Id.ToString(),
                Text = r.Title
            }).ToList();

            var emptyItem = new SelectListItem()
            {
                Value = "",
                Text = "--选择父级菜单--"
            };

            lMenus.Insert(0, emptyItem);

            ViewBag.ParentMenus = new SelectList(lMenus, "Value", "Text");
            
        }


        [HttpPost]
      //   [Authorize(Roles = "创始人,管理员")]
        public JsonResult Add(Menu menu)
        {
            

            if (ModelState.IsValid)
            {
                _menuService.Create(menu);
                return Json(true, JsonRequestBehavior.DenyGet);
            }
            else
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }
            //   return RedirectToAction("Index");

        }

      //   [Authorize(Roles = "创始人,管理员")]
        public ActionResult Edit(int Id)
        {
            GetParentMenu();
            var vMenu = _menuService.GetById(Id);

            if (vMenu != null)
            {
                return View(vMenu);
            }
            return View("NotFound");
        }

        [HttpPost]
      //   [Authorize(Roles = "创始人,管理员")]
        public JsonResult Edit(Menu Menu)
        {


            //carousel.UpdatedBy = User.Identity.Name;
            //carousel.UpdatedDate = DateTime.Now;   

            if (ModelState.IsValid)
            {
                _menuService.Update(Menu);
                return Json(true, JsonRequestBehavior.DenyGet);

            }
            else
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }

        }

        // POST: Admin/User/Delete
        [HttpPost]     
        //  [Authorize(Roles = "创始人,管理员")]
        public JsonResult Delete(int id)
        {

            Menu vMenu = _menuService.GetById(id);

            if (vMenu != null)
            {
                //string imgurl = Server.MapPath(vMenu.ImageUrl);
                //if (System.IO.File.Exists(imgurl))
                //{
                //    System.IO.File.Delete(imgurl);    //删除缩略图
                //}

                _menuService.Delete(vMenu);
              
                return Json(true, JsonRequestBehavior.DenyGet);
            }

            return Json(false, JsonRequestBehavior.DenyGet);
        }

        [HttpPut]
        public JsonResult ActiveMenu(int id)
        {

            Menu vMenu = _menuService.GetById(id);

            try
            {
                if (vMenu != null)
                {
                    vMenu.Active = !vMenu.Active;

                    _menuService.Update(vMenu);
                    //_unit.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                return Json(false, JsonRequestBehavior.DenyGet);
            }

            return Json(true, JsonRequestBehavior.DenyGet);


        }

    }
}
